package com.mls.fivedistrict.controller.backend;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.pojo.RolePermissionDict;
import com.mls.fivedistrict.server.RolePermissionDictService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by nanji on 2018/3/20.
 */
@RestController
@RequestMapping("/api/manager")
public class RolePermissionDictController {
    @Autowired
    RolePermissionDictService rolePermissionDictService;

    @GetMapping("/v1/rolePermissionDicts")
    public ServiceResponse getRolePermissionList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = rolePermissionDictService.selectList(Condition.create().eq("is_delete", false));
        PageInfo pageInfo = new PageInfo(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/rolePermissionDict/all")
    public ServiceResponse getAll() {
        List list = rolePermissionDictService.selectList(Condition.create().eq("is_delete", false).eq("is_enable", true));
        return ServiceResponse.createSuccessResponse(list);
    }

    @GetMapping("/v1/rolePermissionDict/{rolePermissionDictId}")
    public ServiceResponse getRolePermissionDict(@PathVariable("rolePermissionDictId") Long rolePermissionDictId) {
        RolePermissionDict rolePermissionDict = rolePermissionDictService.selectOne(Condition.create().eq("id", rolePermissionDictId));
        return ServiceResponse.createSuccessResponse(rolePermissionDict);
    }

    @PostMapping("/v1/rolePermissionDict")
    public ServiceResponse insertRolePermissionDict(@RequestBody RolePermissionDict rolePermissionDict) {
        BeanValidator.check(rolePermissionDict);
        boolean insert = rolePermissionDictService.insert(rolePermissionDict);
        if (!insert) {
            return ServiceResponse.createErrorResponse("操作失败");
        }
        return ServiceResponse.createSuccessResponse();
    }

    @PutMapping("/v1/rolePermissionDict/{rolePermissionDictId}")
    public ServiceResponse editRolePermissionDict(@PathVariable("rolePermissionDictId") Long rolePermissionDictId, @RequestBody RolePermissionDict rolePermissionDict) {
        rolePermissionDict.setId(rolePermissionDictId);
        boolean update = rolePermissionDictService.updateById(rolePermissionDict);
        if (!update) {
            return ServiceResponse.createErrorResponse("操作失败");
        }
        return ServiceResponse.createSuccessResponse();
    }

    /**
     * 软删除，使权限标志为删除
     *
     * @param rolePermissionDictId
     * @return
     */
    @DeleteMapping("/v1/rolePermissionDict/{rolePermissionDictId}")
    public ServiceResponse deleteRolePermissionDict(@PathVariable("rolePermissionDictId") Long rolePermissionDictId) {
//        boolean delete = rolePermissionDictService.deleteById(rolePermissionDictId);
        RolePermissionDict rolePermissionDict = new RolePermissionDict();
        rolePermissionDict.setId(rolePermissionDictId);
        rolePermissionDict.setIsDelete(true);
        boolean update = rolePermissionDictService.updateById(rolePermissionDict);
        if (!update) {
            return ServiceResponse.createErrorResponse("操作失败");
        }
        return ServiceResponse.createSuccessResponse("操作成功");
    }


}
